<?php

class DiagramManageController extends Controller {
    var $name = 'DiagramManage';
    var $uses = array('Diagram', 'Customfields');
    var $components = array('diagram', 'template');

    function index() {
        $this->action = 'diagram_manage';
        $this->diagram_manage();
    }

    function diagram_manage($status = null) {
        if ($status == 'edit_done') {
            $this->header_notice = __('Edit done.');
        } else if ($status == 'diagram_delete_done') {
            $this->header_notice = __('Delete done.');
        }
        $this->set('diagram_list', get_diagram_as_ul(array('manage'=>true)));
    }

    function diagram_new($id = null, $status = null) {
        $page_content_style = 'display:none;';
        $customfields_style = 'display:none;';
        $templates_tr_style = 'display:none;';
        $is_custom_template_style = 'display:none;';
        $list_type_style = 'display:none;';
        $select_template_style = '';
        $submit_title = __('Add new');
        $post_content = '';
        $id = (int)$id;
        $parent_id = null;
        $current_id = null;
        $customfields = array();
        $tr_count = 0;
        $templates = array();

        # get the parent id
        if (!empty($id) && !empty($status)) {
            switch($status) {
                case 'add_next_item':
                    $parent_id = $id;
                    break;
                case 'add_done':
                    $diagram = $this->Diagram->read('parent', $id);
                    $parent_id = $diagram->parent;
                    $this->header_notice = __('Add done.') . ' ' . admin_link(__('View or edit'), "/diagram_manage/diagram_new/$id/edit/?/diagram_manage/diagram_new/$id/add_done/");
                    break;
            }
        }

        # if is edit
        if (!empty($id) && $status == 'edit') {
            $id = (int)$id;
            $diagram = $this->Diagram->read('*', $id);
            if (!empty($diagram)) {
                if (!empty($this->referer)) {
                    $redirect = $this->referer;
                } else {
                    $redirect = '/diagram_manage/diagram_manage/edit_done';
                }
                # save edit
                if (!empty($this->data)) {
                    $this->data['Diagram']['id'] = $id;
                    $this->data['Diagram']['title'] = sanitize_title($this->data['Diagram']['title']);
                    $this->data['Diagram']['content'] = $this->data['Posts']['post_content'];
                    $this->data['Diagram']['parent'] = (int)$this->data['Diagram']['parent'];
                    $this->data['Diagram']['url'] = $this->data['Diagram']['url'];

                    # get the metavalue
                    if (!empty($this->data['Diagram']['is_custom_template'])) {
                        $metavalue['template'] = sanitize_title($this->data['Diagram']['custom_template']);
                    } else {
                        $metavalue['template'] = isset($this->data['Diagram']['template']) ? stripslashes($this->data['Diagram']['template']) : '';
                    }
                    $metavalue['list_type'] = isset($this->data['Diagram']['list_type']) ? $this->data['Diagram']['list_type'] : '';
                    $metavalue['is_custom_template'] = $this->data['Diagram']['is_custom_template'];
                    $this->data['Diagram']['metavalue'] = addslashes(serialize($metavalue));

                    $this->Diagram->save($this->data);
                    $this->save_customfields($id);
                    al_cache_urls();
                    $this->redirect($redirect);
                }
                $current_id = $id;
                $parent_id = $diagram->parent;
                $this->data['Diagram']['type'] = $diagram->type;
                $this->data['Diagram']['title'] = $diagram->title;
                $post_content = $diagram->content;
                $this->data['Diagram']['parent'] = $diagram->parent;
                $this->data['Diagram']['url'] = $diagram->url;
                $submit_title = __('Save edit');
                
                # set the meta value 
                $metavalue = unserialize($diagram->metavalue);

                foreach($metavalue as $key => $value) {
                    $this->data['Diagram'][$key] = $value;
                }

                if ($diagram->type == 'page') {
                    $page_content_style = '';
                    $templates_tr_style = '';
                    $templates = get_templates_as_array('page');
                }
                if (!empty($this->data['Diagram']['is_custom_template'])) {
                    $is_custom_template_style = '';
                    $select_template_style = 'display:none;';
                    $this->data['Diagram']['custom_template'] = $this->data['Diagram']['template'];
                }
                # set the customfields
                if ($diagram->type == 'list') {
                    $customfields_style = '';
                    $templates_tr_style = '';
                    $list_type_style = '';
                    $templates = get_templates_as_array('list');
                    $customfields = $this->Customfields->find_all("diagram_id='$id'", '*', "ORDER BY field_order ASC");
                    $tr_count = count($customfields) -1;
                    if ($tr_count < 0) {
                        $tr_count = 0;
                    }
                } else if ($diagram->type = 'item') {
                    $templates_tr_style = '';
                }
            }
            # add new diagram
        } else if (!empty($this->data['Diagram']['type'])) {
            switch($this->data['Diagram']['type']) {
                case 'page':
                    break;
                case 'list':
                    break;
                case 'item':
                    break;
                case 'url':
                    break;
            }
            $this->data['Diagram']['title'] = sanitize_title($this->data['Diagram']['title']);
            $this->data['Diagram']['content'] = $this->data['Posts']['post_content'];
            $this->data['Diagram']['parent'] = (int)$this->data['Diagram']['parent'];
            $this->data['Diagram']['url'] = sanitize_title($this->data['Diagram']['url']);
            $this->data['Diagram']['status'] = 1;

            # get the metavalue
            if (!empty($this->data['Diagram']['is_custom_template'])) {
                $metavalue['template'] = sanitize_title($this->data['Diagram']['custom_template']);
            } else {
                $metavalue['template'] = isset($this->data['Diagram']['template']) ? stripslashes($this->data['Diagram']['template']) : '';
            }
            $metavalue['list_type'] = isset($this->data['Diagram']['list_type']) ? $this->data['Diagram']['list_type'] : '';
            $metavalue['is_custom_template'] = $this->data['Diagram']['is_custom_template'];
            $this->data['Diagram']['metavalue'] = addslashes(serialize($metavalue));

            $this->Diagram->save($this->data);
            $diagram_id = $this->Diagram->get_last_insert_id();
            # save the sustomfields
            $this->save_customfields($diagram_id);
            # save the display order
            $this->Diagram->id = $diagram_id;
            $this->Diagram->save_field('displayorder', $diagram_id);
            $parent_id = $this->data['Diagram']['parent'];
            $count = $this->Diagram->get_count("parent='$parent_id'", 'id');
            # update the parent's url
            if ($count == 1) {
                $parent = $this->Diagram->read('`type`', $parent_id);
                if ($parent->type == 'item') {
                    #$this->Diagram->save_field('url', $this->data['Diagram']['url'], "id='$parent_id'");
                }
            }
            al_cache_urls();
            $this->redirect('/diagram_manage/diagram_new/' . "$diagram_id/add_done/");
        }

        $diagram_select_option_list = get_diagram_as_select(array('selected'=>$parent_id, 'diagram'=>true, 'none'=>__('None'), 'current_id'=>$current_id));
        $this->set('diagram_select_option_list', $diagram_select_option_list);

        # set the custom fields
        $this->set('customfields', $customfields);
        $this->set('tr_count', $tr_count);
        $this->set('customfields_style', $customfields_style);

        # set the page value
        $this->set('post_content', $post_content);
        # set the page content style
        $this->set('page_content_style', $page_content_style);

        # set the templates
        $this->set('templates', $templates);
        $this->set('templates_tr_style', $templates_tr_style);
        $this->set('is_custom_template_style', $is_custom_template_style);
        $this->set('select_template_style', $select_template_style);

        # set the category list type
        $this->set('list_type_style', $list_type_style);

        # set the submit title
        $this->set('submit_title', $submit_title);

        # set the successful message
        if ($status == 'page_new_done') {
            $this->header_notice = __('Add new page done.');
        } else if ($status == 'category_new_done') {
            $this->header_notice = __('Add new category done.');
        } else if ($status == 'page_edit_done') {
            $this->header_notice = __('Edit page done.');
        } else if ($status == 'category_edit_done') {
            $this->header_notice = __('Edit category done.');
        }
    }

    function move_up($id) {
        $this->layout = null;
        if (empty($id)) {
            return null;
        }
        diagram_move($id, 'moveup');
        al_cache_urls();
        $this->redirect('/diagram_manage/diagram_manage/move_up_done/#diagram_' . $id);
        exit();
    }

    function move_down($id) {
        $this->action = 'diagram';
        if (empty($id)) {
            return null;
        }
        diagram_move($id, 'movedown');
        al_cache_urls();
        $this->redirect('/diagram_manage/diagram_manage/move_down_done/#diagram_' . $id);
        exit();
    }

    function diagram_delete($diagram_id) {
        $diagram_id = (int)$diagram_id;
        if (!empty($diagram_id)) {
            diagram_delete($diagram_id);
            $this->Customfields->del_all("diagram_id='$diagram_id'");
        }
        al_cache_urls();
        $this->redirect('/diagram_manage/diagram_manage/diagram_delete_done');
    }

    function save_customfields($diagram_id) {
        # 删除原有的字段,然后重新创建
        $this->Customfields->del_all("diagram_id='$diagram_id'");
        if (!empty($this->data['Fields']['title']) && !empty($this->data['Fields']['key'])) {
            $data = array();
            $data['Customfields']['diagram_id'] = $diagram_id;
            foreach($this->data['Fields']['title'] as $key => $value) {
                if (!empty($value) && !empty($this->data['Fields']['key'][$key])) {
                    $data['Customfields']['field_title'] = $value;
                    $data['Customfields']['field_key'] = $this->data['Fields']['key'][$key];
                    $data['Customfields']['field_order'] = (int)$this->data['Fields']['order'][$key];
                    $this->Customfields->save($data);
                }
            }
        }
    }

    function get_templates($type = '') {
        $this->layout = null;
        $type = trim($type);
        if (empty($type)) {
            return '';
        }
        get_templates($type);
    }

}
?>
